package com.oxchinx.order.message;

import com.esotericsoftware.minlog.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 接收mq消息
 */
@Slf4j
@Component
public class MqReceiver {
    /**
     * 1. 需要在rabbitListener页面手动创建myQueue队列
     * @RabbitListener(queues = "myQueue")
     * */
    @RabbitListener(queues = "myQueue")
    public void process(String message){
        log.info("MqReceiver: {}",message);
    }

    /**
     * 2. 自动创建队列
     * @RabbitListener
     * */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("myQueue1"),
            exchange = @Exchange("myExchange1")
    ))
    public void process1(String message){
        log.info("MqReceiver: {}",message);
    }


    /**
     * Binding：绑定，它的作用就是把exchange和queue按照路由规则绑定起来。
     * Exchange：消息交换机，它指定消息按什么规则，路由到哪个队列。
     * Routing Key：路由关键字，exchange根据这个关键字进行消息投递。
     */

    /**
     * 3.1 数码供应商服务，接受消息
     * key：分组，只接收computer相关的消息
     * @RabbitListener
     * */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange("myOrder"),
            key = "computer",
            value = @Queue("computerOrder")

    ))
    public void computerOrderProcess(String message){
        log.info("computerOrderMqReceiver: {}",message);
    }

    /**
     * 3.2 水果供应商服务，接受消息
     * key：分组，只接收fruit相关的消息
     * @RabbitListener
     * */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange("myOrder"),
            key = "fruit",
            value = @Queue("fruitOrder")

    ))
    public void fruitOrderProcess(String message){
        log.info("fruitOrderMqReceiver: {}",message);
    }

}
